﻿/*
创建人：zhunan
日期：2023.1.16
描述：自定义对象子项维护js
*/
var page = {
  pagetotal: 0,
  pageSize: 12,
  pageNumber: 1,
  mainUrl: getRootPath() + "/Ashx/CustomObjectItemsMaint.ashx", //本页面后台路径
  //初始化页面
  init: function () {
    page.bind();
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    // 批次信息
    $("#tbCustomObject").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'customobjectid', title: '父id', hidden: true },
        { field: 'customobjectitemsid', title: '唯一id', hidden: true },
        { field: 'customobjectname', title: '父对象名称', width: 100 },
        { field: 'customobjectitemsname', title: '后缀名称', width: 100 },
        { field: 'customobjectitemsdesc', title: '后缀说明', width: 100 },
        { field: 'customobjectitemsnotes', title: '备注', width: 100 },
        { field: 'useqty', title: '在用批次', width: 60 },
        {
          field: 'isuse', title: '启用状态', width: 60,
          formatter: function (value, row, index) {
            if (value === 0) {
              return '启用';
            } else {
              return '停用';
            }
          },
          styler: function (value) {
            if (value === 1) {
              return 'color:red;';
            } else {
              return 'color:green;';
            }
          }
        },
        { field: 'creater', title: '创建人', width: 70 },
        {
          field: 'createdate', title: '创建时间', width: 150,
          formatter: function (value, row, index) {
            if (value) {
              return value.replace('T', ' ');
            } else {
              return value;
            }
          }
        },
        { field: 'updater', title: '更新人', width: 70 },
        {
          field: 'lastchangedate', title: '更新时间', width: 150,
          formatter: function (value, row, index) {
            if (value) {
              return value.replace('T', ' ');
            } else {
              return value;
            }
          }
        }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        });
        customizedEasyuiPagination('pp'); //定制化分页
      },
      //行点击事件
      onClickRow: function (rowIndex, rowData) { }
    });

    //批次表格分页
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize;
        page.pageNumber = pageNumber;
        page.search();
      }
    });

    //绑定分页go按钮
    $(function () {
      $('#pageGo').on("click", function () {
        var pageNumber = $(this).parents('tr').find('.pagination-num').val();
        page.pageNumber = pageNumber;
        page.search();
      });
    });

    // 查询
    $("#btnSearch").on("click", function () {
      page.pageNumber = 1;
      page.search();
    });

    // 重置按钮
    $('#btnReset').on("click", function () {
      page.resetData();
    });

    // 添加
    $("#btnSave").on("click", function () {
      page.insertData();
    });

    // 更新
    $("#btnUpdate").on("click", function () {
      var datas = $("#tbCustomObject").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要更新的记录！", false);
        return;
      }
      if (datas.length >= 2) {
        displayMessage("每次只能更新1条数据", false);
        return;
      }
      var useqty = datas[0].useqty;
      if (useqty >= 1) {
        displayMessage("使用中，不能修改!", false);
        return;
      }
      $("#ddlDialogCumtomObject").combobox('select', datas[0].customobjectid)
      $("#txtDialogCustomObjectItemsName").textbox('setValue', datas[0].customobjectitemsname);
      $("#txtDialogCustomObjectItemsDesc").textbox('setValue', datas[0].customobjectitemsdesc);
      $("#txtDialogCustomObjectItemsNotes").textbox('setValue', datas[0].customobjectitemsnotes);
      $('#dialog').window('open');
    });

    //停用/启用
    $('#btnToggleIsuse').on("click", function () {
      var datas = $("#tbCustomObject").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要停用/启用的记录！", false);
        return;
      }
      page.toggleIsuse(datas);
    });

    // 删除
    $("#btnDel").on("click", function () {
      var datas = $("#tbCustomObject").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要删除的记录！", false);
        return;
      }
      if (datas.length >= 2) {
        displayMessage("一次只能勾选1条记录!", false);
        return;
      }
      // var hasInUseItmes = datas.every(function (item) { return item.useqty === 0 });
      // if (!hasInUseItmes) {
      //   displayMessage("存在使用中的批次后缀，不能删除!", false);
      //   return;
      // }
      $.messager.confirm('删除前确认', '你确定要删除吗?', function (r) {
        // 确认删除
        if (r) {
          page.deleteData(datas);
        }
      });
    });

    // 对话框 保存 按钮
    $("#btnDialogSave").on('click', function () {
      var datas = $("#tbCustomObject").datagrid('getChecked');
      var customobjectitemsid = datas[0].customobjectitemsid;
      var customobjectitemsname = $("#txtDialogCustomObjectItemsName").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//后缀标识
      var customobjectitemsdesc = $("#txtDialogCustomObjectItemsDesc").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//后缀描述
      var customobjectitemsnotes = $("#txtDialogCustomObjectItemsNotes").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//备注
      var customobjectid = $("#ddlDialogCumtomObject").combobox('getValue');
      var customobjectname = $("#ddlDialogCumtomObject").combobox('getText');
      if (!customobjectitemsname) {
        $("#dialogMessage").text('消息：后缀标识不能为空').css('color', 'red');
        return;
      }
      if (!customobjectitemsdesc) {
        $("#dialogMessage").text('消息：后缀描述不能为空').css('color', 'red');
        return;
      }
      var data = {
        action: "updateData",
        customobjectitemsid: customobjectitemsid,//后缀唯一id
        customobjectitemsname: customobjectitemsname,
        customobjectitemsdesc: customobjectitemsdesc,
        customobjectitemsnotes: customobjectitemsnotes,
        customobjectid: customobjectid, //父对象id
        customobjectname: customobjectname//父对象名称
      }
      page.updateData(data);
    });

    // 对话框 关闭 按钮
    $("#btnDialogClose").on('click', function () {
      $('#dialog').window('close');
      page.search(1);
    });
  },
  //初始化页面
  initData: function () {
    page.getCustomObject();
  },
  // 查询自定义对象
  getCustomObject: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=getCustomObject",
      data: { action: "getCustomObject" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          res.DBTable.unshift({ customobjectid: '', customobjectname: '' });
          $("#ddlCumtomObject").combobox({
            valueField: 'customobjectid',
            textField: 'customobjectname',
            panelHeight: 200,
            data: res.DBTable
          });
          $("#ddlInputCumtomObject").combobox({
            valueField: 'customobjectid',
            textField: 'customobjectname',
            panelHeight: 200,
            data: res.DBTable
          });
          $("#ddlDialogCumtomObject").combobox({
            valueField: 'customobjectid',
            textField: 'customobjectname',
            panelHeight: 200,
            data: res.DBTable
          });
        }
      }
    });
  },
  // 查询
  search: function (pageNumber) {
    $('#tbCustomObject').datagrid('loadData', []);
    var createdateStart = "";
    if (!isNaN(dayjs($("#createdateStart").datebox('getValue')).$y)) {
      createdateStart = $("#createdateStart").datebox('getValue');
    }
    var createdateEnd = "";
    if (!isNaN(dayjs($("#createdateEnd").datebox('getValue')).$y)) {
      createdateEnd = $("#createdateEnd").datebox('getValue');
    }
    var formData = {
      action: "getData",
      pageSize: page.pageSize,
      pageNumber: pageNumber || page.pageNumber,
      customobjectid: $('#ddlCumtomObject').combobox('getValue'),//父项id
      customobjectitemsname: $("#txtCustomObjectItemsName").textbox('getValue'),//子项名称
      creater: $("#txtCreateEmp").textbox('getValue'),
      createdateStart: createdateStart,
      createdateEnd: createdateEnd,
      isuse: $('#ddlIsuse').combobox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=getData",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbCustomObject').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbCustomObject').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbCustomObject').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbCustomObject').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 添加
  insertData: function () {
    var customObjectItemsName = $("#txtInputCustomObjectItemsName").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//子项名称
    var customObjectItemsDesc = $("#txtInputCustomObjectItemsDesc").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//子项描述
    var customObjectItemsNotes = $("#txtInputCustomObjectItemsNotes").textbox('getValue').replace(/(^\s*)|(\s*$)/g, "");//备注
    var isuse = $('#ddlInputIsuse').combobox('getValue');//启用状态
    var customObjectId = $('#ddlInputCumtomObject').combobox('getValue');//父项id
    var customObjectName = $('#ddlInputCumtomObject').combobox('getText');//父项名称
    if (!customObjectId) {
      displayMessage("请选择父项名称！", false);
      return;
    }

    if (!customObjectItemsName) {
      displayMessage("后缀标识不能为空！", false);
      return;
    }
    if (!customObjectItemsDesc) {
      displayMessage("后缀描述不能为空！", false);
      return;
    }

    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=insertData",
      data: {
        action: "insertData",
        customObjectItemsName: customObjectItemsName,
        customObjectItemsDesc: customObjectItemsDesc,
        customObjectItemsNotes: customObjectItemsNotes,
        customObjectId: customObjectId,
        customObjectName: customObjectName,
        isuse: isuse
      },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('添加成功！', true);
          $("#txtInputCustomObjectItemsName").textbox('clear');
          $("#txtInputCustomObjectItemsDesc").textbox('clear');
          $("#txtInputCustomObjectItemsNotes").textbox('clear');
          $('#ddlInputIsuse').combobox('select', 0);
          $('#ddlInputCumtomObject').combobox('select', '');
          page.search();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 更新
  updateData: function (data) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=updateData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('更新成功!', true);
          $('#dialog').window('close');
          page.search();
        } else {
          displayMessage(res.Message, false);
          $('#dialog').window('close');
        }
      }
    });
  },
  // 删除数据
  deleteData: function (selectdata) {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=deleteData",
      data: {
        action: "deleteData",
        willBeDeleteData: JSON.stringify(selectdata)
      },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('删除成功！', true);
          page.search();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 停用/启用切换
  toggleIsuse: function (data) {
    var data = {
      action: "toggleIsuse",
      selectData: JSON.stringify(data)
    };

    $.ajax({
      type: "post",
      url: page.mainUrl + "?action=toggleIsuse",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('停用/启用切换成功!', true);
          page.search();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 清除方法，重要！！！
  reset: function (iscondition, isresult) {
    clearMessage();
    if (isresult) {
      clearEasyuiDataGrid("tbCustomObject", 1);
    }
  },
  // 重置
  resetData: function () {
    $('#ddlCumtomObject').combobox('select', '');
    $("#txtCustomObjectItemsName").textbox('clear');
    $("#txtCreateEmp").textbox('clear');
    $("#createdateStart").datebox('clear');
    $("#createdateEnd").datebox('clear');
    $('#ddlIsuse').combobox('select', '全部');
    $("#txtInputCustomObjectItemsName").textbox('clear');
    $("#txtInputCustomObjectItemsDesc").textbox('clear');
    $("#txtInputCustomObjectItemsNotes").textbox('clear');
    $('#ddlInputIsuse').combobox('select', 0);
    $('#ddlInputCumtomObject').combobox('select', '');
    $("#tbCustomObject").datagrid('loadData', { total: 0, rows: [] });
    page.pageNumber = 1;
    clearMessage();
  }
};
page.init();